package com.suitsoft.ut.web.middle;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;
import org.springside.modules.web.struts2.annotations.Token;

import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.commons.LoginUser;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.entity.base.Student;
import com.suitsoft.ut.entity.base.Teacher;
import com.suitsoft.ut.entity.base.Team;
import com.suitsoft.ut.entity.commons.Examination;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.base.StudentManager;
import com.suitsoft.ut.service.commons.ExaminationManager;
import com.suitsoft.ut.service.select.SelectManager;
import com.suitsoft.ut.service.thesis.ThesisFileManager;
import com.suitsoft.ut.util.StringUtils;
import com.suitsoft.ut.web.CrudActionSupport;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 * 评阅学生自查
 *@author Suineg
 */
@Results({@Result(name = CrudActionSupport.RELOAD,location="examination-readteacher",type="redirect")})
public class ExaminationReadteacherAction extends InitListActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = -151490853259510858L;

	//Action's Result
	/** 自查表 */
	private final String READ = "read";
	
	
	/** 选题 服务层对象 */
	private SelectManager selectManager;
	/** 流程管理  服务层对象 */
	private ThesisFileManager fileManager;
	/** 自查表管理  服务层对象 */
	private ExaminationManager examinationManager;
	/** 学生管理 服务层对象*/
	private StudentManager studentManager;
	
	/** 题的分页 */
	private Page<Thesis> page = new Page<Thesis>(Constants.PAGE_SIZE);
	/** 教师 */
	private String teacher;
	/** 团队 */
	private String group;
	/** 状态 */
	private Long stauts;
	/** 标识  1 - 评阅,2 - 团队,3 - 专家，4 - 基层 (专家没有评语。所以，专家审核时不会用到这个.)*/
	private Long  flag;
	/** 题目 */
	private String titile;
	/** 教师名 */
	private String teacherName;
	/** 当前登陆用户 */
	private LoginUser loginUser;
	/** 自查表 */
	private Examination examination;
	/** 自查表ID */
	private Long eid;
	/***/
	private List<Thesis> listAll;
	/** 教师编号  */
	private String tid;
	
	
	@Override
	public String execute() throws Exception {
		loginUser = UserHolder.getLoginUser();
		loginTeacher = teacherManger.findTeacherByUser(UserHolder.getLoginUser().getId());
		if(loginTeacher == null){
			addActionMessage("请使用教师账号登陆！");
			return SUCCESS;
		}
		boolean f = false;
		for(Team t : loginTeacher.getTeamList()){
			if(Constants.TEAM_TYPE_MIDDLE.equals(Integer.valueOf(t.getType()))){
				f = true;
			}
		}
		if(!f){
			addActionMessage("您未在中期团队中。不能进行此操作");
			return SUCCESS;
		}
		listAll = selectManager.findExaminationBy(page, titile, teacherName, Constants.THESIS_FLAG_MIDLLE, Constants.THSIS_STAUTS_MIDLLE_READ_WEEK_REPORT,null,2L,loginTeacher);
		return SUCCESS;
	}
	
	/**
	 * 初始化自查表
	 * @return
	 */
	public String examination(){
		if(StringUtils.isEmpty(tid)){
			addActionMessage("非法操作");
			return RELOAD;
		}
		Teacher teacher = teacherManger.findByTeacherNo(tid);
		loginTeacher = teacherManger.findTeacherByUser(UserHolder.getLoginUser().getId());
		if(loginTeacher == null){
			addActionMessage("请使用教师账号登陆！");
			return RELOAD;
		}
		examination = teacher.getExamination();
		if(examination == null){
			addActionMessage("此教师 自查表信息无法找到！请确认此教师是否已经提交自查表");
			return RELOAD;
		}
		Map<String,Examination> studentMap = new HashMap<String,Examination>();
		Student student = null;
		
		//以Map形式整理数据
		//以学号为KEY
		List<Thesis> teachersThesis = thesisManager.findByTeacherNo(teacher.getTeacherNo());
		int i = 0;
		for(Thesis t : teachersThesis){
			if(!StringUtils.isEmpty(t.getStudentNo())){
				student = studentManager.findByStudentNo(t.getStudentNo());
				studentMap.put(student.getStudentNo(), student.getExamination());
				i ++;
			}
		}
		Struts2Utils.getRequest().setAttribute("cnt", i);
		Struts2Utils.getRequest().setAttribute("studentMap", studentMap);
		Struts2Utils.getRequest().setAttribute("teacerThe", teachersThesis);
		Struts2Utils.getRequest().setAttribute("teacher", teacher);
		return READ;
	}
	@LogAnnotation(log = "评阅教师自查表  - 保存评阅")
	@Token
	public String save(){
		if(StringUtils.isEmpty(tid)){
			addActionMessage("非法操作");
			return RELOAD;
		}
		try {
			Teacher teacher = teacherManger.findByTeacherNo(tid);
			loginTeacher = teacherManger.findTeacherByUser(UserHolder.getLoginUser().getId());
			if(teacher == null){
				addActionMessage("请使用教师账号登陆！");
				return RELOAD;
			}
			String content = examination.getTeacherReadContent();
			Long isOK = examination.getTeacherReadIsOk();
			
			examination = teacher.getExamination();
			if(examination == null){
				examination = new Examination();
			}
			examination.setTeacherReadIsOk(isOK);
			examination.setTeacherReadDate(new Date());
			examination.setTeacherReadName(loginTeacher.getTeacherName());
			examination.setTeacherReadContent(content);
			examinationManager.save(examination);
			
			if(examination.getTeacherReadIsOk() == 1L){
				List<Thesis> teachersThesis = thesisManager.findByTeacherNo(teacher.getTeacherNo());
				for(Thesis t : teachersThesis){
					thesisFile = t.getThesisFile();
					thesisFile.setIsExaminationTeacherOk(1L);
					fileManager.save(thesisFile);
				}
			}else{
				List<Thesis> teachersThesis = thesisManager.findByTeacherNo(teacher.getTeacherNo());
				for(Thesis t : teachersThesis){
					thesisFile = t.getThesisFile();
					thesisFile.setIsExaminationTeacherOk(0L);
					fileManager.save(thesisFile);
				}
			}
			fileManager.save(thesisFile);
			addActionMessage("操作成功！");
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败！");
		}
		return RELOAD;
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public ThesisFile getThesisFile() {
		return thesisFile;
	}
	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}
	public Page<Thesis> getPage() {
		return page;
	}
	public void setPage(Page<Thesis> page) {
		this.page = page;
	}
	public String getTeacher() {
		return teacher;
	}
	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	public Long getStauts() {
		return stauts;
	}
	public void setStauts(Long stauts) {
		this.stauts = stauts;
	}
	public Long getFlag() {
		return flag;
	}
	public void setFlag(Long flag) {
		this.flag = flag;
	}
	public String getTitile() {
		return titile;
	}
	public void setTitile(String titile) {
		this.titile = titile;
	}
	public String getTeacherName() {
		return teacherName;
	}
	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public LoginUser getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(LoginUser loginUser) {
		this.loginUser = loginUser;
	}
	@Autowired
	public void setSelectManager(SelectManager selectManager) {
		this.selectManager = selectManager;
	}
	@Autowired
	public void setFileManager(ThesisFileManager fileManager) {
		this.fileManager = fileManager;
	}
	@Autowired
	public void setExaminationManager(ExaminationManager examinationManager) {
		this.examinationManager = examinationManager;
	}
	public Long getEid() {
		return eid;
	}
	public void setEid(Long eid) {
		this.eid = eid;
	}

	public Examination getExamination() {
		return examination;
	}

	public void setExamination(Examination examination) {
		this.examination = examination;
	}

	public List<Thesis> getListAll() {
		return listAll;
	}

	public void setListAll(List<Thesis> listAll) {
		this.listAll = listAll;
	}

	public String getTid() {
		return tid;
	}
	public void setTid(String tid) {
		this.tid = tid;
	}

	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
	}
}

